package com.zuoban.sms.service.impl;


import cn.hutool.json.JSONUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.zuoban.sms.properties.SmsProperties;
import com.zuoban.sms.service.SmsService;
import lombok.RequiredArgsConstructor;

import java.util.Map;


@RequiredArgsConstructor
public class AliyunSmsServiceImpl implements SmsService {

    private final SmsProperties smsProperties;

    @Override
    public void send(Map<String, Object> params, String phone) {
        DefaultProfile profile = DefaultProfile.getProfile(smsProperties.getAliyun().getRegionId(),
                smsProperties.getAliyun().getAccessKeyId(),
                smsProperties.getAliyun().getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(smsProperties.getAliyun().getSignName());
        request.setTemplateCode(smsProperties.getAliyun().getTemplateCode());
        request.setPhoneNumbers(phone);
        request.setTemplateParam(JSONUtil.toJsonStr(params));
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(JSONUtil.toJsonStr(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}
